# echo server
import socket
import select
##################################
#commands as functions go here####
##################################
def help():
    print("helper")
    return "helpstuff"
def farts():
    print("fart test")
    
    #print(self.fartsval)
    return "fartsval: "  +"""
You take a deep breath
and fart.

""" 
class commands():
    #commands dict
	#TODO: eventually tie things together so that commands can do actual actions
##############################################################################################3
    #See dostuff below? ALL COMMANDS HAVE TO GO THERE
    #moredoc later
    #
    #
    #
    #
    #
    #
    #
#########################################################################    
    dostuff = {'help': help,'fart':farts,'buy':'stuff'}
##########################################################################################    
    def read_input(self,data):
        #takes input and reads the dict?
        #data
        holding = data
        inp = str(holding.strip('\n'))
        while inp not in self.dostuff:
            return 'boop: try again' + "\n"
        return self.dostuff[inp]()
        #return self.dostuff[inp] + "\n"
	#hmmm how to handle actual actions.
	# stuff like help can return strings, but other things need to have actions 
	# or be strings that need to look a certain way. "player farts" or "player does x to other_player"
	# before I had a dict that changed a state, I guess I could do that
	# dict with a state number
	# state number tells what to do in another function
	# then that function makes stuff to return.
###########################################################################
#######################################################################
commander = commands()

######################################################################
#server stuff below
####################################################################
class server:
    HOST = ''
    PORT = 50007
    s = ''
    
    connections = []
       
        #with conn:
           # print('Connected by', addr)
            
    def create(self):
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.bind((self.HOST, self.PORT))
        self.s.listen(1)
    def process(self):
        #socket object into list for select
        read_list = [self.s]

        #select to check sockets for ready
        readable, writeable, errored = select.select(read_list, [],[])
        

        #go through sockets that are readable
        if readable:
            for i in readable:
                #if readable obj is in socket "self.s" then we can accept
                if i is self.s:
                    conn, addr = self.s.accept()
                    self.connections.append({'conn': conn, 'addr': addr,'data':[]})
                    print('booop')
                else:
                    data = i.recv(1024)
                    print(data)
                    for c in connections:
                        if i == c['conn']:
                            c['data'].append(data.decode('utf-8'))
                
        if writeable:
            for w in writeable:
                if w is not self.s:
                    #conn= wx['conn']
                    print(conn)
                    copyw =  self.commander.read_data(w['data'])
                    conn.sendall(copyw.encode('utf-8'))
        #for wx in self.connections:
           # print('a')
            #if wx in writeable:
                #print('b')
                #if wx is not self.s:
                    #print('c')
                    
                    #conn= wx['conn']
                   # print(conn)
                    #copyw =  self.commander.read_data(wx['data'])
                   # conn.sendall(copyw.encode('utf-8'))
        print("connections list")
        print(self.connections)
        print("readable")
        print(readable)
        print("writeable")
        print(writeable)
        #print(errored)
        pass
        #conn, addr = self.s.accept()
            #connections.append({'conn': conn, 'addr': addr})
        #return self.s.accept()
        #self.connections
    def read(self):
        print('serverxpy read')
        for r in self.readable:
            #check if it's in readable but NOt a new socket to be made
            for cn in self.connections:
                if r == cn['conn']:
                    indata = cn['conn'].recv(1024)
                    cn['data'].append(indata.decode('utf-8'))
            


    def write(self):
        print('serverxpy write')
        print("connections list")
        print(self.connections)
        print("readable")
        print(self.readable)
        print("writeable")
        print(self.writeable)
        for w in self.writeable:
            for cw in self.connections:
                if w == cw['conn']:
                    copycw = commander.read_data(w['data'])
                    cw['conn'].sendall(copyw.encode('utf-8'))

        #conn.sendall(data.encode('utf-8'))
